<script setup>
import { ref, computed, reactive, onMounted } from 'vue'
import { useRouter, useRoute} from 'vue-router'
import { Search, ShoppingCart, UserFilled } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const route = useRoute()

const searchText = ref('')
//const cartCount = ref(0) // 这里应该从store中获取实际的购物车数量
const activeIndex = ref('/home')
//获取userStore对象
const userStore = useUserStore()
onMounted(() => {
  //console.log('userStore.user.avatarUrl',userStore.user.avatarUrl)
    // 初始化时根据userStore中的数据设置state
   state.name = userStore.user.uname || '用户中心'
   state.uimage = userStore.user.uimage || ''
   
})

const state = reactive({
    name:  '用户中心',
    uimage:  '' ,
    hasBack: false
})
// 根据当前路由设置活动菜单
const activeMenu = computed(() => {
  const path = route.path
  if (path.includes('home')) return 'home'
  if (path.includes('address')) return 'personal'
  if (path.includes('personal')) return 'personal'
  if (path.includes('order')) return 'personal'
  if (path.includes('cart')) return 'personal'
  return 'home'
})

// 获取当前页面名称
const getCurrentPageName = () => {
  const path = route.path
  if (path.includes('address')) return '收货地址管理'
  if (path.includes('personal')) return '个人中心'
  if (path.includes('order')) return '订单管理'
  if (path.includes('cart')) return '购物车'
  if (path.includes('search')) return '搜索结果'
  if (path.includes('product')) return '商品详情'
  return '首页'
}

// 处理菜单选择
const handleMenuSelect = (index) => {
  switch (index) {
    case 'home':
      router.push('/home')
      break
    case 'all':
      // 全部商品 - 跳转到搜索页显示所有商品
      router.push('/search?keyword=a')
      break
    case 'men':
      router.push(`/search?keyword=男`)
      break
    case 'women':
      router.push(`/search?keyword=女`)
      break
    case 'sports':
      router.push(`/search?keyword=运动`)
      break
    case 'casual':
      router.push(`/search?keyword=休闲`)
      break
    case 'formal':
      router.push(`/search?keyword=正`)
      break
      case 'new':
       handleSearchNew()
        break
        case 'sale':
          handleSearchSale()  
     break
    default:
      router.push('/home')
  }
}
// 处理新品上市搜索
const handleSearchNew = () => {
  router.push({
    path: '/search',
    query: { type: 'new' }
  })
}

// 处理特价促销搜索
const handleSearchSale = () => {
  router.push({
    path: '/search',
    query: { type: 'sale', isOnSale: 'true' }
  })
}

// 处理新品和特价搜索（整合功能）
const handleSearchNewale = () => {
  router.push({
    path: '/search',
    query: { type: 'new' }
  })
}

// 处理搜索
const handleSearch = () => {
  if (searchText.value.trim()) {
    router.push(`/search?keyword=${encodeURIComponent(searchText.value.trim())}`)
  }
}

// 跳转到购物车
const goToCart = () => {
  router.push('/cart')
}

// 退出登录
const btnLogout = () => {
    //store.saveToken('')
    router.push('/login')
}

// 路由守卫
router.afterEach((to) => {
    activeIndex.value = to.path // 更新活动菜单项
    state.hasBack = ['level2', 'level3'].includes(to.name)
})
</script>

<template>
  <!-- 全局顶部导航栏 -->
  <div class="global-header">
    <div class="container">
      <!-- 顶部导航内容 -->
      <div class="header-top">
        <div class="logo">
          <img src="/src/image/logo.png" alt="SHOEZ SHOES" class="logo-img" />
          <span class="brand-name">SHOEZ SHOES</span>
        </div>
        
        <div class="search-section">
          <el-input
            v-model="searchText"
            placeholder="搜索您想要的鞋子..."
            class="search-input"
            size="large"
            @keydown.enter="handleSearch"
          >
            <template #append>
              <el-button :icon="Search" @click="handleSearch" />
            </template>
          </el-input>
        </div>
        
        <div class="user-actions">
          <!-- 购物车按钮 -->
          <!-- <el-badge :value="cartCount" class="cart-badge">
            <el-button :icon="ShoppingCart" circle size="large" class="cart-btn" @click="goToCart" />
          </el-badge> -->

          <el-button :icon="ShoppingCart" circle size="large" class="cart-btn" @click="goToCart" />
          
          <!-- 用户中心下拉菜单 -->
          <el-dropdown>
            <span class="user-info">
              <el-avatar size="default" :icon="UserFilled" :src="state.uimage" />   
              <span>{{ state.name }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item><router-link to="/orderManagement">我的订单</router-link></el-dropdown-item>
                <el-dropdown-item><router-link to="/personal">个人资料</router-link></el-dropdown-item>
                <el-dropdown-item><router-link to="/address">收货地址</router-link></el-dropdown-item>
                <el-dropdown-item divided @click="btnLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      
      <!-- 主菜单 -->
      <div class="nav-menu">
        <el-menu
          :default-active="activeMenu"
          class="main-menu"
          mode="horizontal"
          background-color="#ff8c00"
          text-color="#fff"
          active-text-color="#fff"
          @select="handleMenuSelect"
        >
          <el-menu-item index="home">首页</el-menu-item>
          <el-menu-item index="all">全部商品</el-menu-item>
          <el-menu-item index="men">男鞋</el-menu-item>
          <el-menu-item index="women">女鞋</el-menu-item>
          <el-menu-item index="sports">运动鞋</el-menu-item>
          <el-menu-item index="casual">休闲鞋</el-menu-item>
          <el-menu-item index="formal">正装鞋</el-menu-item>
          <el-menu-item index="new">新品上市</el-menu-item>
          <el-menu-item index="sale">特价促销</el-menu-item>
        </el-menu>
      </div>
    </div>
  </div>
  
  <!-- 面包屑导航 -->
  <div class="breadcrumb-section">
    <div class="container">
      <el-breadcrumb separator=">" class="breadcrumb">
        <el-breadcrumb-item>
          <router-link to="/home">首页</router-link>
        </el-breadcrumb-item>
        <el-breadcrumb-item v-if="currentPath !== '/home'">
          {{ getCurrentPageName() }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
  </div>
</template>
  
<style scoped>
/* 全局头部样式 */
.global-header {
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

/* 搜索栏样式 */
.search-section {
  flex: 1;
  max-width: 600px;
  margin: 0 40px;
}

.search-input {
  width: 100%;
  height: 45px;
  border-radius: 7px;
  border: 2px solid #ff8c00;
  transition: all 0.3s ease;
}

.search-input:focus {
  border-color: #ff758c;
  box-shadow: 0 0 0 2px rgba(255, 120, 140, 0.2);
}

.search-input .el-input__wrapper {
  border-radius: 23px;
  padding: 0 15px;
}

.search-input .el-input__suffix {
  right: 10px;
}

.search-input .el-button {
  color: #ff8c00;
  border: none;
  background: transparent;
  transition: color 0.3s ease;
}

.search-input .el-button:hover {
  color: #ff758c;
  background: transparent;
}

.container {
  width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-img {
  width: 40px;
  height: 40px;
}

.brand-name {
  font-size: 20px;
  font-weight: bold;
  color: #333;
}

.search-section {
  flex: 1;
  max-width: 600px;
  margin: 0 40px;
}

.search-input {
  width: 100%;
}

.user-actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

.cart-badge {
  margin-right: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: #666;
}

/* 导航菜单样式 */
.nav-menu {
  background-color: #ff8c00;
}

.main-menu {
  border-bottom: none !important;
}

.main-menu .el-menu-item {
  color: #fff !important;
}

.main-menu .el-menu-item.is-active {
  background-color: rgba(255, 255, 255, 0.2) !important;
}

/* 面包屑导航样式 */
.breadcrumb-section {
  background-color: #fff;
  padding: 10px 0;
  margin-top: 2px;
}

.breadcrumb {
  font-size: 14px;
}
</style>
